package com.tang.javaSwingCaculate.Stack;

/**
 * @author tmh
 * @date 2022/12/13 21:20
 * @description
 */
public class test2 {
    public static void main(String[] args) {
        // 要求二进制表示的小数
        double decimal = 5;
        MyStack myStackD = new MyStack(100);
        MyStack myStackP = new MyStack(100);
        if (decimal < 0) {
            decimal = Math.abs(decimal);
            // 用栈来保存二进制数

//            myStackD.push("-");
            int dec = (int) decimal;
            double part = decimal - dec;
            while (dec > 0) {
                myStackD.push(String.valueOf(dec % 2));
                dec = dec / 2;
            }
            while (part > 0) {
                double r = part * 2;
                if (r >= 1) {
                    myStackP.push("1");
                    part = r - 1;
                } else {
                    myStackP.push("0");
                    part = r;
                }
            }
            if (myStackP.length() == 0) {
                String s = "-";
                while (!myStackD.isMyStackEmpty()) {
                    s = s + myStackD.pop();
                }
                System.out.println(s);

            } else {
                String s = "-";
                while (!myStackD.isMyStackEmpty()) {
                    s = s + myStackD.pop();
                }
                s = s + ".";
                for (int i = 0; i < myStackP.length(); i++) {
                    s = s + myStackP.getStackArray()[i];
                }
                System.out.println(s);

            }

        } else {
            int dec = (int) decimal;
            double part = decimal - dec;
            while (dec > 0) {
                myStackD.push(String.valueOf(dec % 2));
                dec = dec / 2;
            }
//            for (int i = 0; i < myStackD.length(); i++) {
//                System.out.print(myStackD.getStackArray()[i]);
//            }
//            String p = "";

            while (part > 0) {
                double r = part * 2;
                if (r >= 1) {
//                    p += "1";
                    myStackP.push("1");
                    part = r - 1;
                } else {
//                    p += "0";
                    myStackP.push("0");
                    part = r;
                }
            }
            if (myStackP.length() == 0) {
                String s = "";
                while (!myStackD.isMyStackEmpty()) {
                    s = s + myStackD.pop();
                }
                System.out.println(s);

            } else {
                String s = "";
                while (!myStackD.isMyStackEmpty()) {
                    s = s + myStackD.pop();
                }
                s = s + ".";
                for (int i = 0; i < myStackP.length(); i++) {
                    s = s + myStackP.getStackArray()[i];
                }
                System.out.println(s);

            }
        }
/*        // 用栈来保存二进制数
        MyStack myStack = new MyStack(100);
        int dec = (int) decimal;
        double part = decimal - dec;
        while (dec > 0) {
            myStack.push(String.valueOf(dec % 2));
            dec = dec / 2;
        }
        for (int i = 0; i < myStack.length(); i++) {
            System.out.print(myStack.getStackArray()[i]);
        }
        String p = "";

        while (part > 0) {
            double r = part * 2;
            if (r >= 1) {
                p += "1";
                part = r - 1;
            } else {
                p += "0";
                part = r;
            }
        }
        System.out.println(p);*/


    }
}
